package com.haidong.controller;

import com.haidong.domain.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/books")
public class BookController {
    // 可以直接使用value注解获取配置文件中的数据,比较零散
    @Value("${lesson}")
    private String lesson;

    @Value("${server.port}")
    private Integer port;

    @Value("${enterprise.subject[0]}")
    private String subject_00;

    @Autowired
    private Enterprise enterprise;

    // 也可以直接用自动装配把配置文件所有数据封装到对象中
    //  实际开发中配置文件数据庞大,不常使用
    @Autowired
    private Environment env;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("id ==> " + id);
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(subject_00);
        System.out.println("-----------------");
        System.out.println(env.getProperty("lesson"));
        System.out.println(env.getProperty("enterprise.name"));
        System.out.println(env.getProperty("enterprise.subject[0]"));
        return "hello , spring boot!";
    }

    @GetMapping
    public String getAll(){
        System.out.println(enterprise.getAge());
        System.out.println(enterprise.getName());
        System.out.println(enterprise.getSubject()[0]);
        System.out.println(enterprise.getTel());
        return "hello , spring boot!";
    }
}
